#encoding:utf-8
'''
Created on 2012-6-13

@author: dahai
'''
import sys
import os,time,re,urllib,urllib2,cookielib
from BeautifulSoup import BeautifulSoup
import socket,threading
import ftplib
import subprocess
import My_ReUrllib,sys
from PyQt4 import QtGui, QtCore, QtWebKit,Qt
from PyQt4.QtGui import *  
from PyQt4.QtCore import * 
import random
from PyQt4 import QtGui,QtCore
import string

class Image_Browser(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('Image Browser')
        url=QtGui.QLabel('GOOGLE Image:')
        open=QtGui.QPushButton('OPEN')
        stop=QtGui.QPushButton('STOP')
        self.connect(open,QtCore.SIGNAL('clicked()'),self.ok)
        self.connect(stop,QtCore.SIGNAL('clicked()'),self.stop)
        self.url=QtGui.QLineEdit()
        grid=QtGui.QGridLayout()
        grid.addWidget(url,2,0)
        grid.addWidget(self.url,2,1,1,3) 
        grid.addWidget(open,3,2)
        grid.addWidget(stop,3,3)
        self.setLayout(grid) 
        self.timer=QtCore.QTimer()
        self.imageView=False 
        self.first=False  
        self.timer=False   
    def Browse_image(self,url):
        self.timer=QtCore.QTimer()
        self.image_links=[]
        #url=r'http://www.google.com.hk/search?q=%E8%A1%97%E6%8B%8D&hl=zh-CN&newwindow=1&safe=strict&biw=1440&bih=791&tbs=isz:l&tbm=isch&prmd=imvnsu&ei=tGd2T7beH4mfiAfs8pnaBA&start=160&sa=N'
        #req=urllib2.Request(url,headers)
        next_pages=[]
        next_pages.append(url)
        cj=cookielib.LWPCookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj),urllib2.HTTPHandler)
        urllib2.install_opener(opener)
        opener.addheaders=[('User-agent','Mozilla/5.0'),("Accept","*/*")]
        html=opener.open(url).read()
        soup=BeautifulSoup(html)
        count=0
        
        self.timer.connect(self.timer, SIGNAL('timeout()'),self.open)
        self.timer.start(10000)
        #得到下一页地址加入next_pages
        for next_page in soup.findAll('a',{'href':re.compile('^/search.+sa=N')}):
            n_p='http://www.google.com.hk'+next_page['href']
            if n_p not in next_pages:
                next_pages.append(n_p)
        spans=soup.findAll('span')
        for span in spans:
            span_html=str(span)
            if '下一页' in span_html:
                print next_pages[-1]
                htmp=opener.open(next_pages[-1]).read()
                soup=BeautifulSoup(html)
            else:
                break
        for url in next_pages:
            html=opener.open(url).read()
            soup=BeautifulSoup(html)
            for link in soup.findAll('a',{'href':re.compile('^/imgres')}):
                l=re.findall(r'imgurl=(http://.*?)&',link['href'])[0]
                self.image_links.append(l)   
    def open(self):
        if not self.imageView:
            self.imageView = QtWebKit.QWebView() 
        url=self.image_links.pop()
        self.imageView.load(QtCore.QUrl(url))
        self.imageView.show()
        
    def ok(self):
        self.input=unicode(self.url.text())
        print self.input
        t=QThread(self.Browse_image(self.input))
        t.run()
    def stop(self):
        if self.timer:
            self.timer.destroyed()
        sys.exit()
class Web_Browser(QtGui.QDialog):
    def __init__(self,parent=None):
        print "init"
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('Web Browser')
        url=QtGui.QLabel('URL:')
        open=QtGui.QPushButton('OPEN')
        self.connect(open,QtCore.SIGNAL('clicked()'),self.ok)
        self.url=QtGui.QLineEdit()
        grid=QtGui.QGridLayout()
        grid.addWidget(url,2,0)
        grid.addWidget(self.url,2,1,1,3) 
        grid.addWidget(open,3,3)
        self.setLayout(grid) 
        self.timer=QtCore.QTimer() 
        self.first=False     
    def main(self,url):
        #url=r'http://www.google.com.hk/search?q=%E8%A1%97%E6%8B%8D&hl=zh-CN&newwindow=1&safe=strict&biw=1440&bih=791&tbs=isz:l&tbm=isch&prmd=imvnsu&ei=tGd2T7beH4mfiAfs8pnaBA&start=160&sa=N'
        #req=urllib2.Request(url,headers)
        self.next_pages=[]
        cj=cookielib.LWPCookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        opener.addheaders=[('User-agent','Mozilla/5.0'),("Accept","*/*")]
        html=opener.open(url).read()
        soup=BeautifulSoup(html)
        count=0
        #print str(soup)
        #得到下一页地址加入next_pages
        for self.next_page in soup.findAll('a',{'href':re.compile('.+')}):
            n_p=self.next_page['href']
            if n_p not in self.next_pages:
                self.next_pages.append(n_p)
        self.timer.connect(self.timer, SIGNAL('timeout()'),self.open)
        self.timer.start(10000)
    def open(self):
        if not self.first:
            self.webView = QtWebKit.QWebView()
            self.webView.load(QtCore.QUrl(self.input))
            self.first=True
        else:
            i=random.randrange(0,self.next_pages.__len__(),1)
            url=self.next_pages[i]
            self.webView.load(QtCore.QUrl(url))
        self.webView.show()
    def ok(self):
        self.input=unicode(self.url.text())
        print self.input
        t=QThread(self.main(self.input))
        t.run()
            
class FTP_Dialog(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('FTP Downloader')
        ftp_Addr=QtGui.QLabel('FTP Addr:')
        ftp_Dir=QtGui.QLabel('FTP Dir:')
        local_Dir=QtGui.QLabel('Local Dir:')
        user_Name=QtGui.QLabel('User Name:')
        password=QtGui.QLabel('PASSWORD:')
        
        self.ftp_Addr=QtGui.QLineEdit()
        self.ftp_Dir=QtGui.QLineEdit()
        self.local_Dir=QtGui.QLineEdit()
        self.user_Name=QtGui.QLineEdit()
        self.password=QtGui.QLineEdit()
        
        grid =QtGui.QGridLayout()
        grid.addWidget(ftp_Addr,1,0)
        grid.addWidget(self.ftp_Addr,1,1,1,3) 
        grid.addWidget(ftp_Dir,2,0)
        grid.addWidget(self.ftp_Dir,2,1,1,3)
        grid.addWidget(local_Dir,3,0)
        grid.addWidget(self.local_Dir,3,1,1,3)
        grid.addWidget(user_Name,4,0)
        grid.addWidget(self.user_Name,4,1)
        grid.addWidget(password,4,2)
        grid.addWidget(self.password,4,3)  
        
        upload=QtGui.QPushButton('UpLoad')
        download=QtGui.QPushButton('DownLoad')
        grid.addWidget(upload,5,2)
        grid.addWidget(download,5,3) 
        
        self.connect(upload, QtCore.SIGNAL('clicked()'),self.Upload)
        self.connect(download, QtCore.SIGNAL('clicked()'),self.Download)
        self.setLayout(grid)        
        
    def get_dirs_files(self):
        dir_res=[]
        self.conn.dir('.',dir_res.append)
        files=[f.split(None,8)[-1]for f in dir_res if f.startswith('-')]
        dirs=[f.split(None,8)[-1]for f in dir_res if f.startswith('d')]
        print files
        return (files,dirs)
    def walk(self,next_dir):
        print 'Walking to',next_dir
        self.conn.cwd(next_dir)
        try:
            os.mkdir(next_dir)
        except OSError:
            pass
        os.chdir(next_dir)
        ftp_curr_dir=self.conn.pwd()
        local_curr_dir=os.getcwd()
        files,dirs=self.get_dirs_files()
        print "FILES:",files
        print "DIRS:",dirs
        for f in files:
            print next_dir,':',f
            outf=open(f,'wb')
            try:
                self.conn.retrbinary('RETR %s'%f,outf.write)
            finally:
                outf.close()
        for d in dirs:
            os.chdir(local_curr_dir)
            self.conn.cwd(ftp_curr_dir)
            self.walk(d)
    def down_run(self):
        self.walk('.')
    def Download(self):
        #self.conn=ftplib.FTP('ftp.sjtu.edu.cn','anonymous','')
        self.conn=ftplib.FTP(unicode(self.ftp_Addr.text()),unicode(self.user_Name.text()),unicode(self.password.text()))
        self.conn.cwd(unicode(self.ftp_Dir.text()))
        os.chdir(unicode(self.local_Dir.text()))
        t=threading.Thread(target=self.down_run)
        t.start()
    def Upload(self):
        pass

#Bbs Dialog Class
#author:chenwei
class Bbs_Dialog(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('Post Bbs')
        self.Lbbsid=QtGui.QLabel('UserName:')
        self.Lpasswd=QtGui.QLabel('PassWord:')
        self.bbsopen=QtGui.QPushButton('OPEN')
        self.bbsstop=QtGui.QPushButton('STOP')
        self.connect(self.bbsopen,QtCore.SIGNAL('clicked()'),self.Post)
        self.connect(self.bbsstop,QtCore.SIGNAL('clicked()'),self.Stop)
        self.bbsid=QtGui.QLineEdit()
        self.passwd=QtGui.QLineEdit()
        grid=QtGui.QGridLayout()
        grid.addWidget(self.Lbbsid,2,0)
        grid.addWidget(self.bbsid,2,1,1,3)
        grid.addWidget(self.Lpasswd,3,0)
        grid.addWidget(self.passwd,3,1,1,3)
        grid.addWidget(self.bbsopen,4,2)
        grid.addWidget(self.bbsstop,4,3)
        self.setLayout(grid) 
        self.timer=QtCore.QTimer()
        self.imageView=False 
        self.first=False  
        self.timer=False
    def Post(self):
        bbsid=unicode(self.bbsid.text())
        passwd=unicode(self.passwd.text())
        t=QThread(self.Bbs_Post(bbsid,passwd))
        t.run()
    def Stop(self,bbsid,passwd):
        sys.exit(self.exec_())
    def Bbs_Post(self,bbsid,passwd):
        cookie = cookielib.MozillaCookieJar()
        cj=urllib2.HTTPCookieProcessor(cookie)
        bbs_login_url='http://bbs.cqupt.edu.cn/nForum/login'
        CookieDate=1
        comeurl=''
        postdata=urllib.urlencode({
            'id':bbsid,
            'passwd':passwd,
            'CookieDate':1,
            'selectlist':1,
            'comeurl':comeurl})
        req=urllib2.Request(bbs_login_url,postdata)
        opener = urllib2.build_opener(cj)
        urllib2.install_opener(opener)
        bbs_url='http://bbs.cqupt.edu.cn/nForum'
        f=opener.open(req)
        forum=opener.open(bbs_url)
        ss=forum.read()
        p = re.compile( r'<a.+?href=.+?>.+?</a>' )
        phref = re.compile( r'(?<=href\=\"/nForum/article).*?(?=\")')
        post_phref=re.compile(r'(?<=href\=\"/nForum/article).*?(?=\d)')
        bbsurl = p.findall(ss)
        j=0
        for i in bbsurl:
            if phref.findall(i) != [] and phref.findall(i) !=[''] and j>154 and j<163:
                print "---------------------------------------------------------------"
                resulturl = 'http://bbs.cqupt.edu.cn/nForum/article'+str(''.join(phref.findall(i))).strip()
                print resulturl
                posturl = '/nForum/article'+str(''.join(post_phref.findall(i))).strip()+"post"
                postconnection='http://bbs.cqupt.edu.cn/nForum/article'+str(''.join(post_phref.findall(i))).strip()+"post"
                soup=BeautifulSoup(opener.open(resulturl).read())
                for value in soup.findAll('input',{'name':re.compile('subject')}):
                    subject = value['value'].replace('%','%25')
                for value in soup.findAll('input',{'name':re.compile('reid')}):
                    reid=value['value'].encode("utf-8",'ignore')
                content='Create python world'
                params = "content="+content+"&reid="+reid+"&subject="+subject
                data={'content':content,'reid':reid,'subject':subject}
                postreq=urllib2.Request(postconnection)
                postdata=urllib.urlencode(data)
                response=opener.open(postreq,postdata)
                break
            j = j+1

#Vedio Dialog Class
#author:chenwei
class Vedio_Dialog(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('vedio')
        self.url=QtGui.QLabel('Input vedio url:')
        self.bbsopen=QtGui.QPushButton('DOWNLOAD')
        self.bbsstop=QtGui.QPushButton('STOP')
        self.connect(self.bbsopen,QtCore.SIGNAL('clicked()'),self.Ok)
        self.connect(self.bbsstop,QtCore.SIGNAL('clicked()'),self.Stop)
        self.inputurl=QtGui.QLineEdit()
        grid=QtGui.QGridLayout()
        grid.addWidget(self.url,2,0)
        grid.addWidget(self.inputurl,2,1,1,3)
        grid.addWidget(self.bbsopen,3,2)
        grid.addWidget(self.bbsstop,3,3)
        self.setLayout(grid) 
        self.timer=QtCore.QTimer()
        self.imageView=False 
        self.first=False  
        self.timer=False
    def report_hook(self,cout,block_size,total_size):
        print ('%0f%%'%(100.0*cout*block_size/total_size))
    def output(self,s):
        sys.stderr.write(s + "\n")
    def DownloadVideo(self,inputurl):
        videourl = str(inputurl)
        format='super'
        url = 'http://www.flvcd.com/parse.php?kw=' + urllib.quote(videourl)  + '&format=' + format;
        req = urllib2.Request(url);
        req.add_header('host', 'www.flvcd.com');
        req.add_header('Referer', url[:-4]);
        req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0');
        req.add_header('Accept-Language', 'en-us,en;q=0.5');
        req.add_header('Accept-Encoding', 'gzip, deflate');
        req.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
        req.add_header('Keep-Alive', '115');
        res = urllib2.urlopen(req);
        html = res.read()
        pattern = re.compile('<input\s+type="hidden"\s+name="inf"\s+value="([^"]+)');
        firstmatch = pattern.search(html);
        urls = firstmatch.group(1);
        urls = unicode(urls, 'gbk'); # urls turns out to be utf8 encoding
        urlpattern = re.compile('<[NU]>(.+)');
        result = urlpattern.findall(urls);
        data = [result[i:i+2] for i in range(0, len(result), 2)]
        count = len(data)
        global files
        files = []
        mytime=[1,2,3,4]
        self.output('\n--- Start to download from url "%s" (%d block(s) in total):' % (videourl, count))
        for k, v in enumerate(data):
            self.output('  >downloading Block %.2d of %.2d ...' % (k+1, count))
            My_ReUrllib.Myurlretrieve(v[1],v[0] + '.flv', reporthook=self.report_hook)
            files.append( (v[0] + '.flv').replace('"', '\\"').replace('$', '\$').encode('gb2312') )
            print('  downloaded Block.%.2d completely<' % (k+1,))
            print('--- finished ---\n')
            print('"' + '" "'.join(files) + '"')
    def Ok(self):
        inputurl=unicode(self.inputurl.text())
        t=QThread(self.DownloadVideo(inputurl))
        t.run()
    def Stop(self):
        sys.exit()

#Renren Class
#author:chenwei
from sgmllib import SGMLParser   
import cookielib
#SGMLParser
class Renren_Dialog(QtGui.QDialog,SGMLParser):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self)
        self.resize(550,400)
        self.setWindowTitle('Post Bbs')
        self.Remail=QtGui.QLabel('UserName:')
        self.Rpassword=QtGui.QLabel('PassWord:')
        self.renrenlogin=QtGui.QPushButton('LOGIN')
        self.renrenstop=QtGui.QPushButton('STOP')
        self.connect(self.renrenlogin,QtCore.SIGNAL('clicked()'),self.Ok)
        self.connect(self.renrenstop,QtCore.SIGNAL('clicked()'),self.Stop)
        self.email=QtGui.QLineEdit()
        self.password=QtGui.QLineEdit()
        grid=QtGui.QGridLayout()
        grid.addWidget(self.Remail,2,0)
        grid.addWidget(self.email,2,1,1,3)
        grid.addWidget(self.Rpassword,3,0)
        grid.addWidget(self.password,3,1,1,3)
        grid.addWidget(self.renrenlogin,4,2)
        grid.addWidget(self.renrenstop,4,3)
        self.setLayout(grid) 
        self.timer=QtCore.QTimer()
        self.imageView=False 
        self.first=False  
        self.timer=False
        #SGMLParser init
        SGMLParser.__init__(self)
        self.h3=False
        self.h3_is_ready=False
        self.div=False
        self.h3_and_div=False
        self.a=False
        self.depth=0
        self.names=""
        self.dic={}   
        self.domain='renren.com'
        try:
            cookie=cookielib.CookieJar()
            cookieProc=urllib2.HTTPCookieProcessor(cookie)
        except:
            raise
        else:
            opener=urllib2.build_opener(cookieProc)
            urllib2.install_opener(opener)
    def login(self,email,password):
        url='http://www.renren.com/PLogin.do'
        postdata={
                  'email':email,
                  'password':password,
                  'domain':self.domain  
                  }
        req=urllib2.Request(
                            url,
                            urllib.urlencode(postdata)            
                            )
        
        self.file=urllib2.urlopen(req).read()
        self.feed(self.file)
        self.show()
    def start_h3(self,attrs):
        self.h3 = True
    def end_h3(self):
        self.h3=False
        self.h3_is_ready=True
        
    def start_a(self,attrs):
        if self.h3 or self.div:
            self.a=True
    def end_a(self):
        self.a=False
        
    def start_div(self,attrs):
        if self.h3_is_ready == False:
            return
        if self.div==True:
            self.depth += 1
            
        for k,v in attrs:
            if k == 'class' and v == 'content':
                self.div=True;
                self.h3_and_div=True   #h3 and div is connected
    def end_div(self):
        if self.depth == 0:
            self.div=False
            self.h3_and_div=False
            self.h3_is_ready=False
            self.names=""
        if self.div == True:
            self.depth-=1
    def handle_data(self,text):
        #record the name
        if self.h3 and self.a:
            self.names+=text
        #record says
        if self.h3 and (self.a==False):
            if not text:pass
            else: self.dic.setdefault(self.names,[]).append(text)
            return 
        if self.h3_and_div:
            self.dic.setdefault(self.names,[]).append(text)
            
    def show(self):
        type = sys.getfilesystemencoding()
        for key in self.dic:
            print ( (''.join(key)).replace(' ','')).decode('utf-8').encode(type), \
                  ( (''.join(self.dic[key])).replace(' ','')).decode('utf-8').encode(type)
    
    def Ok(self):
        email=unicode(self.email.text())
        password=unicode(self.password.text())
        t=QThread(self.login(email,password))
        t.run()
    def Stop(self):
        if self.timer:
            self.timer.destroyed()
        sys.exit()
    
'''
程序主框架 
'''   
class MainFrame(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('Network Stream Generator')
        screen=QtGui.QDesktopWidget().screenGeometry()                 
        self.resize(screen.width(),screen.height())        
        self.statusBar()
        self.show()
                               
        image_Browser=QtGui.QAction(QtGui.QIcon('../Icons/picture.png'),'Image Browser',self)
        image_Browser.setShortcut('Ctrl+I')
        image_Browser.setStatusTip('Image Browser')
        image_Browser.connect(image_Browser,QtCore.SIGNAL('triggered()'),self.image_Browser)
        
        web_Browser=QtGui.QAction(QtGui.QIcon('../Icons/web.png'),'Web Browser',self)
        web_Browser.setShortcut('Ctrl+W')
        web_Browser.setStatusTip('Web  Browser')
        web_Browser.connect(web_Browser,QtCore.SIGNAL('triggered()'),self.web_Browser)

        #ftp
        ftp_Data=QtGui.QAction(QtGui.QIcon('../Icons/FTP.png'),'FTP Data',self)
        ftp_Data.setShortcut('Ctrl+F')
        ftp_Data.setStatusTip('FTP Data')
        ftp_Data.connect(ftp_Data,QtCore.SIGNAL('triggered()'),self.ftp_Instance)
        
        email_Data=QtGui.QAction(QtGui.QIcon('../Icons/email.png'),'E_mail Data',self)
        email_Data.setShortcut('Ctrl+E')
        email_Data.setStatusTip('email Data')
        email_Data.connect(email_Data,QtCore.SIGNAL('triggered()'),QtGui.qApp,QtCore.SLOT('quit()'))

        #vedio
        vedio_Data=QtGui.QAction(QtGui.QIcon('../Icons/video.png'),'Video Data',self)
        vedio_Data.setShortcut('Ctrl+V')
        vedio_Data.setStatusTip('Video Data')
        vedio_Data.connect(vedio_Data,QtCore.SIGNAL('triggered()'),self.vedio_Post)
        #renren
        renren_Data=QtGui.QAction(QtGui.QIcon('../Icons/renren.png'),'Renren Data',self)
        renren_Data.setShortcut('Ctrl+R')
        renren_Data.setStatusTip('Renren Data')
        renren_Data.connect(renren_Data,QtCore.SIGNAL('triggered()'),self.renren_Post)
        #bbs
        bbs_Data=QtGui.QAction(QtGui.QIcon('../Icons/b1.png'),'Bbs Data',self)
        bbs_Data.setShortcut('Ctrl+B')
        bbs_Data.setStatusTip('Bbs Data')
        bbs_Data.connect(bbs_Data,QtCore.SIGNAL('triggered()'),self.bbs_Post)
        
        menubar=self.menuBar()
        app_Layer=menubar.addMenu('&Application Layer')
        app_Layer.addAction(image_Browser)
        app_Layer.addAction(web_Browser)
        app_Layer.addAction(ftp_Data)
        app_Layer.addAction(email_Data)
        app_Layer.addAction(vedio_Data)
        app_Layer.addAction(renren_Data)
        app_Layer.addAction(bbs_Data)
        trans_Layer=menubar.addMenu('&Transport Layer')
        
        net_Layer=menubar.addMenu('&Network Layer')
        
        toolbar=self.addToolBar('bar')
        toolbar.addAction(ftp_Data)
        toolbar.addAction(web_Browser)
        toolbar.addAction(renren_Data)
        toolbar.addAction(email_Data)
        toolbar.addAction(vedio_Data)
        toolbar.addAction(image_Browser)
        toolbar.addAction(bbs_Data)
        
    def ftp_Instance(self):
        ftp=FTP_Dialog()
        ftp.exec_()
    def web_Browser(self):
        web=Web_Browser()
        web.exec_()
    def image_Browser(self):
        image=Image_Browser()
        image.exec_()
    def bbs_Post(self):
        bbs=Bbs_Dialog()
        bbs.exec_()
    def vedio_Post(self):
        video=Vedio_Dialog()
        video.exec_()
    def renren_Post(self):
        renren=Renren_Dialog()
        renren.exec_()
                
app=QtGui.QApplication(sys.argv)
app.setFont(QtGui.QFont("Helvetica", 16))
main=MainFrame()
main.show()

sys.exit(app.exec_())

        
